package com.rockcent.third;

import com.aliyun.oss.OSSClient;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ning.http.client.AsyncHttpClient;
import com.rockcent.common.BaseConfig;
import com.rockcent.common.common.RDruidDataSource;
import com.rockcent.mall.lib.AutoLoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import javax.sql.DataSource;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Configuration
public class Config extends BaseConfig{

    /**
     * 公共线程池
     * @return
     */
    @Bean
    ExecutorService commonExecutorService() {
        return Executors.newFixedThreadPool(5);
    }

    @Bean
    AsyncHttpClient asyncHttpClient() {
        return new AsyncHttpClient();
    }
    @Bean
    ObjectMapper objectMapper() {
        return new ObjectMapper();
    }

    /**
     * sql 操作 NamedParameterJdbcTemplate
     * @param dataSource
     * @return
     */
    @Bean
    NamedParameterJdbcTemplate namedParameterJdbcTemplate(DataSource dataSource) {
        return new NamedParameterJdbcTemplate(dataSource);
    }

    /**
     * 数据库连接池
     * @return
     */
    @Bean
    public DataSource druidDataSource() {
        return RDruidDataSource.getInstance(prop.jdbcUrl, prop.jdbcUsername, prop.jdbcPassword);
    }
    /**
     * 自动登录 interceptor
     * @return
     */
    @Bean
    public AutoLoginInterceptor autoLoginInterceptor(){
        return new AutoLoginInterceptor(prop.userToken);
    }



    /**
     * 阿里云 oss 服务
     * @return
     */
    @Bean
    OSSClient ossClient(){
        return new OSSClient(prop.aliyunOssEndpoint, prop.aliyunOssAccessKeyId, prop.aliyunOssaccessKeySecret);
    }

    /**
     * 配置文件
     */
    @Autowired
    private Prop prop;
}